﻿using System;
using YKXiangQi.Enums;
using YKXiangQi.Models;

namespace YKXiangQi.Common
{
    public class Helper
    {
        public static string GetMoveLocalNotation(Piece[,] pieces, Move move)
        {
            int x1, x2;
            int distance;
            var direction = string.Empty;
            var y1 = move.From.Y;
            var y2 = move.To.Y;
            var piece = pieces[move.From.Y, move.From.X];

            if (piece == null) return string.Empty;

            if ( piece.Side == Sides.Red)
            {
                x1 = 9 - move.From.X;
                x2 = 9 - move.To.X;
            }
            else
            {
                x1 = move.From.X + 1;
                x2 = move.To.X + 1;
            }

            if (y1 == y2)
            {
                direction = "-";
                distance = x2;
            }
            else
            {
                if (piece.Side == Sides.Red)
                    direction = y1 > y2 ? "." : "/";
                else
                    direction = y1 > y2 ? "/" : ".";

                distance = Math.Abs(y1 - y2);
            }

            if (piece is Horse || piece is Elephant || piece is Advisor)
                distance = x2;

            var linePosition = string.Empty;
            var pieceType = piece.GetType();
            int n1 = 0, n2 = 0;

            for (var y = 0; y < 10; y++)
            {
                var p = pieces[y, move.From.X];
                if (p != null && p.GetType() == pieceType
                    && p.Side == piece.Side && p != piece)
                {
                    if (y < move.From.Y)
                        n1++;
                    else if (y > move.From.Y)
                        n2++;
                }
            }

            if (piece.Side == Sides.Red)
            {
                if (n1 > 0 && n2 > 0)
                    linePosition = "(g)";
                else if (n1 > 0)
                    linePosition = "(s)";
                else if (n2 > 0)
                    linePosition = "(t)";
            }
            else
            {
                if (n1 > 0 && n2 > 0)
                    linePosition = "(g)";
                else if (n2 > 0)
                    linePosition = "(s)";
                else if (n1 > 0)
                    linePosition = "(t)";
            }

            var result = string.Format("{0}{1}{2}{3}{4}{5}", piece.LocalNotation, x1, linePosition, direction, distance, move.Type.GetEnumDescription());
            return result;
        }

        public static string GetFen(Piece[,] pieces, Sides side)
        {
            var result = string.Empty;
            for (int y = 0; y < 10; y++)
            {
                int empty = 0;
                for (int x = 0; x < 9; x++)
                {
                    if (pieces[y, x] == null)
                        empty++;
                    else
                    {
                        if (empty > 0)
                        {
                            result += empty.ToString();
                            empty = 0;
                        }

                        result += pieces[y, x].Notation;
                    }
                }
                if (empty > 0)
                    result += empty.ToString();

                result += @"/";
            }

            if (result.EndsWith(@"/"))
                result = result.Substring(0, result.Length - 1);

            result = string.Format("{0} {1}", result, side == Sides.Red ? "w" : "b");

            return result;
        }
    }
}
